<?php

namespace Home\Controller;

use Common\Service\WXBizDataCrypt;
use Common\Service\WXBizDataCryptAes;
use Common\Service\Curl;

class ApiController extends BaseController
{
    /**
     * 通过code获取openid
     */
    public function getopenidbycode()
    {
        $code = I('code');
        $appid = C('APP_ID');
        $secret = C('APP_SECRET');
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
        $json = Curl::request("get", $url);
        $res = json_decode($json);
        $openid = $res->openid;
        if ($openid) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '查询openid成功', 'data' => $openid]);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '查询openid失败']);
        }
    }

    /**
     * 通过openid获取当前用户状态信息
     */
    public function nowOpenidInfo()
    {
        $openid = I('openid');
        $audit = $this->getInfoByOpenid('audit', $openid);
        $manager = $this->getInfoByOpenid('store', $openid);

        // if (!$audit && !$manager) {
        //     $this->ajaxReturn(['status' => false, 'code' => 1004, 'msg' => '信息错误']);
        // }

        if ($audit) {
            $index = 1;
            $userInfo = $audit;
        }

        if ($manager) {
            $index = 2;
            $userInfo = $manager;
        }

        if (!$userInfo) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '当前未绑定用户']);
        } else {
            $tab = $this->getRoleTab()[$index];
            if ($tab == 'audit') {//审核员
                $data = [
                    'name' => $userInfo['name'],
                    'company_name' => $userInfo['company_name'],
                ];
            } elseif ($tab == 'store') {//店长
                $company_name = $this->getStoreCompanyName($userInfo['id']);
                $data = [
                    'name' => $userInfo['manager_name'],
                    'company_name' => $company_name,
                ];
            } else {
                $data = ['数据有误'];
            }

            $data['user_id'] = $userInfo['id'];
            $data['openid'] = $userInfo['openid'];
            $data['mobile'] = $userInfo['mobile'];
            $data['user_type'] = $index;

            $file = M('files')->where(['file_id' => $userInfo['headimg']])->find();
            if ($file) {
                $file_path = C('DOMAIN') . $file['filepath'];
            } else {
                $file_path = $userInfo['headimg'];
            }

            $data['headimg'] = $file_path;
            /*
            $data = [
                'user_id' => $info['id'],
                'name' => $info['name'],
                'company_name' => $info['company_name'],
                'openid' => $info['openid'],
                'user_type' => 2
            ];*/

            $ret = ['status' => true, 'code' => 1000, 'msg' => '用户当前已绑定审核员', 'data' => $data];
            $this->ajaxReturn($ret);
        }
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $openid = I('openid');
        $user_type = I('user_type');

        //审核员
        if ($user_type == 1) {
            $r = M('audit')->where(['openid' => $openid])->save(['openid' => '', 'update_time' => time()]);
        }

        //店长
        if ($user_type == 2) {
            $r = M('store')->where(['openid' => $openid])->save(['openid' => '', 'update_time' => time()]);
        }

        if ($r) {
            $ret = ['status' => true, 'code' => 1000, 'msg' => '已退出'];
            $this->ajaxReturn($ret);
        } else {
            $ret = ['status' => true, 'code' => 1001, 'msg' => '系统异常'];
            $this->ajaxReturn($ret);
        }
    }

    /**
     * 订单状态
     */
    public function orderStatus()
    {
        //未下单  待审核  未通过  已通过  待报价  待确定报价  待接收任务  已接收  生成完成  待上画  上画进行中 上画待验收  待下画 下画进行中  完成
        $this->ajaxReturn(['status' => true, 'code' => 1000, 'data' => C('order_status')]);
    }

    /**
     * 订单状态值
     */
    public function orderStatusMsg()
    {
        $status = I('status');
        $this->ajaxReturn(['status' => true, 'code' => 1000, 'data' => C('order_status')[$status] ? C('order_status')[$status] : '未知状态']);
    }

    /**
     * 跑火车
     * @return [type] [description]
     */
    public function orderInfo(){

        $id = I('id');

        $keyword = I('keyword');

        $tmp = [];

        $info = [
                    0=>'未下单',
                    1=>'待审核',
                    2=>'未通过',
                    3=>'已通过',
                    4=>'待报价',      //11
                    5=>'待确定报价',  //12
                    6=>'待接收任务',
                    7=>'已接收',
                    8=>'生产完成',
                    9=>'待上画',
                    10=>'上画进行中',
                    11=>'上画待验收',
                    12=>'待下画',
                    13=>'下画进行中',
                    14=>'下画待验收',
                    15=>'完成'

                ];

        $where['a.activity_id'] = $id;

        if($keyword != ''){
            $where['b.store_name'] = ['like',"%".$keyword."%"];
        }

        foreach ($info as $k => $v) {
            
            $tmp[$k]['status'] = $k;
            $tmp[$k]['statusMsg'] = $v;
            $tmp[$k]['statusCount'] = 0;

            if(in_array($k, [0,1,2,3,4,5])){

                $status = $k;

                if($k == 4) $status = 11;
                if($k == 5) $status = 12;

                $where['a.order_status'] = $status;

                //店长审核员状态字段
                $res = M('order a')->join("wk_store b on a.store_id=b.id")
                    ->field("b.store_name,b.address,a.order_no,a.activity_id")
                    ->where($where)->select();
            }else{
                //师傅状态
                

                if($k == 6){
                    $wk_status = 0; //待接单
                    $where_['order_status'] = 3;
                }

                if($k == 7){
                    $wk_status = 1; //已接单，待领取物料 
                }

                if($k == 8){
                    $wk_status = 2; //生产完成
                }

                if($k == 9){
                    $wk_status = 3;  //待到店上画
                }

                if($k == 10){
                    //$wk_status = 4; //上画进行中
                }

                if($k == 11){
                    $wk_status = 4;  //上画完成，待验收或者待下画
                }

                if($k == 12){
                    $wk_status = 5;  //待下画
                }

                if($k == 13){
                    $wk_status = 6;  //下画进行中
                }

                if($k == 14){
                    $wk_status = 7;  //下画待验收
                }

                if($k == 15){
                    $wk_status = 8;  //已完成
                }

                $where_['a.worker_order_status'] = $wk_status;
                $where_['a.activity_id'] = $id;
                //$where['a.order_status'] = 3;

                //店长审核员状态字段
                $res = M('order a')->join("wk_store b on a.store_id=b.id")
                    ->field("b.store_name,b.address,a.order_no,a.activity_id")
                    ->where($where_)->select();

                $where_ = [];

            }


            

            $tmp[$k]['statusCount'] = count($res);

            foreach ($res as $kk => $vv) {
                $res[$kk]['statusInfo'] = $info[$k];
            }

            $tmp[$k]['data'] = $res;

            $res = [];

        }

        $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '查询成功', 'data' => $tmp]);
    }
}